package com.sam.equalhash;

import java.util.HashMap;
import java.util.Map;

public class Main {

    /**
     * @功能说明:
     * @param args
     * @返回类型:void
     * @方法名称:main
     * @类名称:Main
     * @文件名称:Main.java
     * @所属包名:com.sam.equalhash
     * @项目名称:effectiveJava
     * @创建时间:2017-6-23 上午11:52:20
     * @作者:SAM QZL
     * @版本:1.0
     */
    public static void main(String[] args) {

        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("String", "sam");
        System.out.println(map.get(new String("String")));
        map.put(new PhoneNumber((short) 530023, (short) 13654), "sam");
        System.out.println(map.get(new PhoneNumber((short) 530023, (short) 13654)));
        map.containsKey("String");
    }

    public static class PhoneNumber {

        private short areaCode;
        private short lineCode;

        public PhoneNumber(short areaCode, short lineCode) {

            this.areaCode = areaCode;
            this.lineCode = lineCode;
        }

        public short getAreaCode() {

            return areaCode;
        }

        public void setAreaCode(short areaCode) {

            this.areaCode = areaCode;
        }

        public short getLineCode() {

            return lineCode;
        }

        public void setLineCode(short lineCode) {

            this.lineCode = lineCode;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {

            final int prime = 31;
            int result = 1;
            result = prime * result + areaCode;
            result = prime * result + lineCode;
            return result;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object obj) {

            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof PhoneNumber)) {
                return false;
            }
            PhoneNumber other = (PhoneNumber) obj;
            if (areaCode != other.areaCode) {
                return false;
            }
            if (lineCode != other.lineCode) {
                return false;
            }
            return true;
        }
    }
}
